package com.cy.ppj.web.admin.enter;

import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.enter.*;
import com.cy.ppj.model.dto.enter.EnterCorporateProfitListSaveDTO;
import com.cy.ppj.model.dto.enter.EnterFinanceInfoListSaveDTO;
import com.cy.ppj.model.dto.shuidai.EnterEvaluateRequestParamDTO;
import com.cy.ppj.model.query.enter.EnterEvaluateRecordListQuery;
import com.cy.ppj.model.query.enter.EnterListQuery;
import com.cy.ppj.model.vo.enter.*;
import com.cy.ppj.service.enter.EnterService;
import com.cy.ppj.service.shuidai.ShuiDaiService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Description 后台-企业税贷管理
 * @Author tanyilin
 * @Date 2019/4/25 14:13
 */
@RestController
@RequestMapping("/admin/enter")
@Api(tags = "后台-企业税贷管理")
public class AdminEnterController extends BaseController {

    @Autowired
    private EnterService enterService;
    @Autowired
    private ShuiDaiService shuiDaiService;

    @ApiOperation("查询企业详情")
    @GetMapping("/get/detail")
    public Result<EnterDetailVO> getEnterDetail(EnterDetailQueryAO queryAO) {
        EnterDetailVO enterDetailVO = enterService.getEnterDetail(queryAO);
        return ok("查询成功", enterDetailVO);
    }

    @ApiOperation("获取企业基本信息")
    @GetMapping("/get/enter/base/info")
    public Result<EnterBaseInfoDetailVO> getEnterBaseInfo(@RequestParam("enterId") Long enterId) {

        EnterBaseInfoDetailVO result = enterService.getEnterBaseInfo(enterId);
        return ok("", result);
    }

    @ApiOperation("保存企业基本信息")
    @PostMapping("/do/save/enter/base/info")
    public Result doSaveEnterBaseInfo(@Validated EnterBaseInfoSaveAO saveAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        enterService.doSaveEnterBaseInfo(saveAO);
        return ok();
    }

    @ApiOperation("获取企业股东信息")
    @GetMapping("/get/shareholder/info")
    public Result<List<EnterShareholderInfoVO>> getEnterShareholderInfo(@RequestParam("enterId") Long enterId) {
        List<EnterShareholderInfoVO> list = enterService.getEnterShareholderInfo(enterId);
        return ok("获取成功", list);
    }

    @ApiOperation("保存企业股东信息")
    @PostMapping("/do/save/shareholder/info")
    public Result doSaveShareholderInfo(@RequestBody @Validated EnterShareholderInfoSaveAO saveAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        enterService.doSaveShareholderInfo(saveAO);
        return ok();
    }

    @ApiOperation("获取企业管理人员信息")
    @GetMapping("/get/manager/info")
    public Result<List<EnterManagerInfoVO>> getEnterManagerInfoVO(@RequestParam("enterId") Long enterId) {
        List<EnterManagerInfoVO> list = enterService.getEnterManagerInfo(enterId);
        return ok("获取成功", list);
    }

    @ApiOperation("保存企业管理人员信息")
    @PostMapping("/do/save/manager/info")
    public Result doSaveManagerInfo(@RequestBody @Validated EnterManagerInfoSaveAO saveAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        enterService.doSaveEnterManagerInfo(saveAO);
        return ok();
    }

    @ApiOperation("获取企业对外投资信息")
    @GetMapping("/get/investment/info")
    public Result<List<EnterInvestmentInfoVO>> getEnterInvestmentInfoVO(@RequestParam("enterId") Long enterId) {
        List<EnterInvestmentInfoVO> list = enterService.getEnterInvestmentInfo(enterId);
        return ok("获取成功", list);
    }

    @ApiOperation("保存企业对外投资信息")
    @PostMapping("/do/save/investment/info")
    public Result doSaveInvestmentInfo(@RequestBody @Validated EnterInvestmentInfoSaveAO saveAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        enterService.doSaveEnterInvestmentInfo(saveAO);
        return ok();
    }

    @ApiOperation("获取企业申报数据明细")
    @GetMapping("/get/declareData/detail")
    public Result<List<EnterDeclareDataDetailVO>> getEnterDeclareDataDetail(@RequestParam("enterId") Long enterId) {
        List<EnterDeclareDataDetailVO> list = enterService.getEnterDeclareDataDetail(enterId);
        return ok("获取成功", list);
    }

    @ApiOperation("保存企业申报数据明细")
    @PostMapping("/do/save/declareData/detail")
    public Result doSaveEnterDeclareDataDetail(@RequestBody @Validated EnterDeclareDataDetailSaveAO saveAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        enterService.doSaveEnterDeclareDataDetail(saveAO);
        return ok();
    }

    @ApiOperation("获取企业财务报表信息")
    @GetMapping("/get/enter/finance/info")
    public Result<Map<String, Object>> getEnterFinanceInfo(@RequestParam("enterId") Long enterId) {
        Map<String, Object> map = enterService.getEnterFinanceInfo(enterId);
        return ok("", map);
    }

    @ApiOperation("导入财务报表")
    @PostMapping("/import/financeInfo/excel")
    public Result importFinanceInfoExcel(@RequestParam(value = "file") MultipartFile file, @RequestParam("enterId") Long enterId) {
        //解析Excel表格数据
        List<EnterFinanceInfoListSaveDTO> financeInfoList = enterService.getFinanceInfoListSaveDTO(file);
        if(CommonUtils.isEmpty(financeInfoList)) {
            return fail("财务报表数据为空");
        }
        //保存财务报表信息
        enterService.doSaveEnterFinanceInfo(financeInfoList, enterId);
        return ok("导入财务报表成功", financeInfoList);
    }

    @ApiOperation("获取企业利润报表信息")
    @GetMapping("/get/enter/corporate/profit")
    public Result<Map<String, Object>> getEnterCorporateProfit(@RequestParam("enterId") Long enterId) {
        Map<String, Object> map = enterService.getEnterCorporateProfit(enterId);
        return ok("", map);
    }

    @ApiOperation("导入利润报表")
    @PostMapping("/import/corporateProfit/excel")
    public Result importCorporateProfitExcel(@RequestParam(value = "file") MultipartFile file, @RequestParam("enterId") Long enterId) {
        //解析Excel表格数据
        List<EnterCorporateProfitListSaveDTO> corporateProfitList = enterService.getCorporateProfitListSaveDTO(file);
        if(CommonUtils.isEmpty(corporateProfitList)) {
            return fail("利润报表数据为空");
        }
        //保存利润报表信息
        enterService.doSaveEnterCorporateProfit(corporateProfitList, enterId);
        return ok("导入利润报表成功", corporateProfitList);
    }

    @ApiOperation("进行企业评估")
    @GetMapping("/do/evaluate")
    public Result doEvaluate(@RequestParam("enterId") Long enterId) {

        //查询并封装请求参数
        Map<String, Object> requestParamMap = enterService.getEnterEvaluateRequestParam(enterId);
        //请求api进行评估
        Result<Map<String, Object>> result = shuiDaiService.postToEvaluate(requestParamMap);
        if(!result.isOk()) {
            return result;
        }
        Map<String, Object> resultMap = result.getValue();
        resultMap.put("enterId", enterId);
        enterService.doSaveEvaluateRecord(resultMap);
        return ok();
    }

    @ApiOperation("企业列表")
    @GetMapping("/list")
    public PageBean<EnterListVO> getEnterList(EnterListQuery query) {
        PageBean<EnterListVO> pageBean = enterService.selectEnterList(query);
        return pageBean;
    }

    @ApiOperation("企业评估记录")
    @GetMapping("/evaluateRecord/list")
    public PageBean<EnterEvaluateRecordListVO> getEnterEvaluateRecordList(EnterEvaluateRecordListQuery query) {
        PageBean<EnterEvaluateRecordListVO> pageBean = enterService.selectEnterEvaluateRecordList(query);
        return pageBean;
    }


}
